package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HjlyNew;
import com.ruoyi.system.service.IHjlyNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻Controller
 * 
 * @author ruoyi
 * @date 2022-03-11
 */
@RestController
@RequestMapping("/hjly/new")
public class HjlyNewController extends BaseController
{
    @Autowired
    private IHjlyNewService hjlyNewService;


    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:list')")
    @GetMapping("/list")
    public TableDataInfo list(HjlyNew hjlyNew)
    {
        startPage();
        List<HjlyNew> list = hjlyNewService.selectHjlyNewList(hjlyNew);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HjlyNew hjlyNew)
    {
        List<HjlyNew> list = hjlyNewService.selectHjlyNewList(hjlyNew);
        ExcelUtil<HjlyNew> util = new ExcelUtil<>(HjlyNew.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:query')")
    @GetMapping(value = "/{newId}")
    public AjaxResult getInfo(@PathVariable("newId") Integer newId)
    {
        return AjaxResult.success(hjlyNewService.selectHjlyNewByNewId(newId));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HjlyNew hjlyNew)
    {
        return toAjax(hjlyNewService.insertHjlyNew(hjlyNew));
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HjlyNew hjlyNew)
    {
        hjlyNew.setUpdateBy(SecurityUtils.getUserId().toString());
        return toAjax(hjlyNewService.updateHjlyNew(hjlyNew));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('hjly:new:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newIds}")
    public AjaxResult remove(@PathVariable Integer[] newIds)
    {
        return toAjax(hjlyNewService.deleteHjlyNewByNewIds(newIds));
    }
}
